﻿using MediatR;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Domains.Saleser;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week03.leos
{
    public class SaleDetaisSuretenHandlet2 : IRequestHandler<SaleDetaisSuretenCommend2, Result>
    {
        private readonly IBaseRepositoryGet<SalesDetails> SalesDetails;
        private readonly IBaseRepositoryGet<Clients> Clients;

        public SaleDetaisSuretenHandlet2(IBaseRepositoryGet<SalesDetails> SalesDetails, IBaseRepositoryGet<Clients> Clients)
        {
            this.SalesDetails = SalesDetails;
            this.Clients = Clients;
        }

        public async Task<Result> Handle(SaleDetaisSuretenCommend2 request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };

            try
            {
                var list = (from a in SalesDetails.GetAll()
                            join b in Clients.GetAll() on a.CustomerId equals b.CustomerId
                            where a.IsDelete == false
                            select new
                            {
                                a.SalesDetailsId,
                                a.SalesDetailsDate,
                                a.SalesDetailsOrderNumber,
                                a.SalesDetailsOrderNum,
                                a.SalesDetailsState,
                                b.CustomerId,
                                b.CustomerNickname,
                            }).ToList();


                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    
    }
}
